﻿using System;
using System.Collections.Specialized;
using System.Web.Mvc;

namespace ActivateYourGlutes.Model
{
    public class ValidationException : Exception
    {
        private readonly NameValueCollection _errors;

        public ValidationException(NameValueCollection errors)
        {
            _errors = errors;
        }

        public ValidationException(string key, string value)
        {
            _errors = new NameValueCollection { { key, value }};
        }

        public NameValueCollection Errors { get { return _errors; } }

        public void AddToModelState(ModelStateDictionary modelState, string prefix)
        {
            foreach(string key in _errors.Keys)
            {
                foreach(var value in _errors.GetValues(key))
                {
                    modelState.AddModelError(String.Format("{0}.{1}", prefix, key), value);
                }
            }
        }

        public void AddToModelState(ModelStateDictionary modelState)
        {
            foreach (string key in _errors.Keys)
            {
                foreach (var value in _errors.GetValues(key))
                {
                    modelState.AddModelError(key, value);
                }
            }
        }
    }
}